﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Exam.Core.DBUtility;
using System.Data;

namespace Exam.Core.SQLDAL
{
    public class QuestionDifficulties {

        /// <summary>
        /// 难度列表 以及该难度下试题数目
        /// </summary>
        /// <param name="p"></param>
        /// <param name="courseid"></param>
        /// <returns></returns>
        internal List<Exam.Core.Model.TacticsQuestionDifficulty> getListByKnowledgeId(string knowid, string typeid, string courseid) {
            List<Exam.Core.Model.TacticsQuestionDifficulty> list = new List<Exam.Core.Model.TacticsQuestionDifficulty>();
            string SQL = "SELECT DISTINCT QuestionDifficultyId FROM Question WHERE QuestionKnowledgeId="+knowid+" AND QuestionTypeId=" + typeid + "  AND CourseId=" + courseid;
            DataTable dt = DbHelperSQL.Query(SQL).Tables[0];
            string path = FilePath.QuestionDifficultyXmlPath.Trim();
            XmlDocument doc = XmlHelper.GetXmlDocument(path);
            XmlNodeList nodeList = doc.GetElementsByTagName("questiondifficulty");
            foreach (DataRow dr in dt.Rows) {
                foreach (XmlNode node in nodeList) {
                    if (dr["QuestionDifficultyId"].ToString().Trim() == node.Attributes["id"].Value.Trim()) {
                        Exam.Core.Model.TacticsQuestionDifficulty obj = new Exam.Core.Model.TacticsQuestionDifficulty();
                        obj.QuestionDifficultyId = node.Attributes["id"].Value.Trim();
                        obj.QuestionDifficultyTitle = node.Attributes["title"].Value.Trim();
                        obj.QuestionCount = getQCount(obj.QuestionDifficultyId, knowid, typeid, courseid);
                        list.Add(obj);
                    }
                }
            }
            return list;
        }

        private string getQCount(string difficultyid,string knowid, string typeid, string courseid) {
            string SQL = "SELECT COUNT(*) FROM Question WHERE QuestionKnowledgeId="+knowid+" AND QuestionDifficultyId=" + difficultyid + " AND QuestionTypeId=" + typeid + " AND CourseId=" + courseid;
            object obj = DbHelperSQL.GetSingle(SQL);
            string result = "0";
            if(obj != null)
                result = obj.ToString();
            return result;
        }
    }
}
